package com.wechat.page.chat.misc;

import com.swak.entity.YesNo;
import com.swak.fx.support.ui.UICell;
import com.swak.fx.support.ui.UILabel;
import com.swak.fx.support.ui.UILifecycle;
import com.swak.fx.support.ui.UISwitch;
import com.wechat.dto.ChatDTO;
import com.wechat.enums.ContactType;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

/**
 * FlagForm.
 * 
 * 属性页面
 * 
 * @author 618lf
 */
public class FlagForm extends GridPane implements UILifecycle {

	private final String[] STYLE_CLASS = { "flag_form" };

	private final Label displayNickNameText;
	private final UISwitch displayNickNameSwitch;
	private final UICell displayNickNameCell;

	private final Label alertText;
	private final UISwitch alertSwitch;
	private final UICell alertCell;

	private final Label topText;
	private final UISwitch topSwitch;
	private final UICell topCell;

	private final Label friendText;
	private final UISwitch friendSwitch;
	private final UICell friendCell;

	public FlagForm() {
		this.getStyleClass().addAll(STYLE_CLASS);

		// 显示群成员昵称
		this.displayNickNameCell = new UICell();
		this.displayNickNameText = new UILabel("显示群成员昵称");
		this.displayNickNameSwitch = new UISwitch();

		// 消息免打扰
		this.alertCell = new UICell();
		this.alertText = new UILabel("消息免打扰");
		this.alertSwitch = new UISwitch();

		// 置顶聊天
		this.topCell = new UICell();
		this.topText = new UILabel("置顶聊天");
		this.topSwitch = new UISwitch();

		// 保存到通讯录
		this.friendCell = new UICell();
		this.friendText = new UILabel("保存到通讯录");
		this.friendSwitch = new UISwitch();

		this.init();
	}

	@Override
	public void initializeParts() {
		this.getChildren().add(this.displayNickNameCell);
		GridPane.setConstraints(this.displayNickNameCell, 0, 0);
		GridPane.setHgrow(this.displayNickNameCell, Priority.ALWAYS);
		GridPane.setVgrow(this.displayNickNameCell, Priority.ALWAYS);

		this.getChildren().add(this.alertCell);
		GridPane.setConstraints(this.alertCell, 0, 1);
		GridPane.setHgrow(this.alertCell, Priority.ALWAYS);
		GridPane.setVgrow(this.alertCell, Priority.ALWAYS);

		this.getChildren().add(this.topCell);
		GridPane.setConstraints(this.topCell, 0, 2);
		GridPane.setHgrow(this.topCell, Priority.ALWAYS);
		GridPane.setVgrow(this.topCell, Priority.ALWAYS);

		this.getChildren().add(this.friendCell);
		GridPane.setConstraints(this.friendCell, 0, 3);
		GridPane.setHgrow(this.friendCell, Priority.ALWAYS);
		GridPane.setVgrow(this.friendCell, Priority.ALWAYS);
	}

	@Override
	public void setupValueChangedListeners() {
		this.chatProperty.addListener((ob, o, n) -> {
			boolean friend = n != null && n.getIsDnn() != null && n.getIsDnn() == YesNo.Yes.getType();
			this.displayNickNameSwitch.setSelected(friend);
		});
		this.chatProperty.addListener((ob, o, n) -> {
			boolean friend = n != null && n.getIsAlert() != null && n.getIsAlert() == YesNo.Yes.getType();
			this.alertSwitch.setSelected(!friend);
		});
		this.chatProperty.addListener((ob, o, n) -> {
			boolean friend = n != null && n.getIsTop() != null && n.getIsTop() == YesNo.Yes.getType();
			this.topSwitch.setSelected(friend);
		});
		this.chatProperty.addListener((ob, o, n) -> {
			boolean friend = n != null && n.getIsFriend() != null && n.getIsFriend() == YesNo.Yes.getType();
			this.friendSwitch.setSelected(friend);
		});
	}

	@Override
	public void setupBindings() {
		this.displayNickNameCell.visibleProperty().bind(this.chatroomProperty);
		this.displayNickNameCell.managedProperty().bind(this.chatroomProperty);
		this.friendCell.visibleProperty().bind(this.chatroomProperty);
		this.friendCell.managedProperty().bind(this.chatroomProperty);
		this.displayNickNameChangeProperty.bind(this.displayNickNameSwitch.selectedProperty());
		this.alertChangeProperty.bind(this.alertSwitch.selectedProperty());
		this.topChangeProperty.bind(this.topSwitch.selectedProperty());
		this.friendChangeProperty.bind(this.friendSwitch.selectedProperty());
	}

	@Override
	public void initializeFinish() {
		this.displayNickNameCell.valueNodeProperty().set(this.displayNickNameText);
		this.displayNickNameCell.opsNodeProperty().set(this.displayNickNameSwitch);

		this.alertCell.valueNodeProperty().set(this.alertText);
		this.alertCell.opsNodeProperty().set(this.alertSwitch);

		this.topCell.valueNodeProperty().set(this.topText);
		this.topCell.opsNodeProperty().set(this.topSwitch);

		this.friendCell.valueNodeProperty().set(this.friendText);
		this.friendCell.opsNodeProperty().set(this.friendSwitch);
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private final ObjectProperty<ChatDTO> chatProperty = new SimpleObjectProperty<>();

	public ObjectProperty<ChatDTO> chatProperty() {
		return chatProperty;
	}

	private BooleanBinding chatroomProperty = Bindings.createBooleanBinding(() -> {
		ChatDTO chat = this.chatProperty.get();
		return chat != null && chat.getType() == ContactType.Chat_Room.getType();
	}, this.chatProperty);

	private final BooleanProperty displayNickNameChangeProperty = new SimpleBooleanProperty();
	private final BooleanProperty alertChangeProperty = new SimpleBooleanProperty();
	private final BooleanProperty topChangeProperty = new SimpleBooleanProperty();
	private final BooleanProperty friendChangeProperty = new SimpleBooleanProperty();

	public BooleanProperty displayNickNameChangeProperty() {
		return displayNickNameChangeProperty;
	}

	public BooleanProperty alertChangeProperty() {
		return alertChangeProperty;
	}

	public BooleanProperty topChangeProperty() {
		return topChangeProperty;
	}

	public BooleanProperty friendChangeProperty() {
		return friendChangeProperty;
	}
}
